import * as dd from "dingtalk-jsapi";
import { apiConfigurable } from "@/request/api/api";
import { closeToast, showLoadingToast, showToast, showSuccessToast, } from "vant";
import { ref } from "vue"

const tourl = window.location.href
const clttype = ref("");
const logintype = ref("");
const ua = ref("")

export const useJssdk = () => {


    function getEnvironment() {
        ua.value = window.navigator.userAgent.toLowerCase();
        logintype.value = "wechat";

        if (ua.value.indexOf("iphone") !== -1) {
            clttype.value = "ios";
        }
        if (ua.value.indexOf("android") !== -1) {
            clttype.value = "android";
        }

        if (ua.value.indexOf("wxwork") !== -1) {
            logintype.value = "wxwork";

            getSignature();
        } else if (ua.value.indexOf("dingtalk") !== -1) {
            logintype.value = "dingding";
            getSignature();
        } else if (ua.value.indexOf("micromessenger") !== -1) {
            logintype.value = "wechat";
            getSignature();
        } else {
            logintype.value = "browser";
        }
    }



    function getSignature() {
        let data = {
            url: tourl,
            type: logintype.value,
            clttype: clttype.value,
        };

        showLoadingToast({
            message: "加载中...",
            forbidClick: true,
            duration: 0,
        });

        const { idobject } = route.query;
        let url = idobject + ".getlocation.do";
        apiConfigurable(url, data).then((res) => {
            if (res.code === 0) {
                let data = res.data;
                if (logintype.value === "weixin") {

                }

                if (logintype.value === "dingding") {
                    ddConfig()

                }
            }
        });
    }

    function ddConfig(data) {
        let configData = {
            agentId: data.agentid,
            corpId: data.appId,
            timeStamp: data.timestamp,
            nonceStr: data.nonceStr,
            signature: data.signature,
            type: 0,
            jsApiList: [
                "runtime.info",
            ],
        };
        dd.config(configData);
        dd.error(function (error) {
            console.log("dingding error" + error);
        });
        dd.ready(function () {
            dd.device.screen.rotateView({
                showStatusBar: true, // 否显示statusbar
                clockwise: true, // 是否顺时针方向
                onSuccess: function (result) {
                },
                onFail: function (err) { }
            });
        });
    }

    return {
        getEnvironment
    }

}

